package com.zznzzn.androidshixun0826;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class ContactLib {
    private static ContactLib holder = null;
    private List<Contact> collection = new ArrayList<>();
    private ContactDBHelper helper;

    public ContactLib(Context context) {
        // select items from db
        helper = ContactDBHelper.getInstance(context);
        flush();
    }

    public ContactLib(List<Contact> list){
        this.collection = list;
    }

    //刷新list
    public void flush() {
        collection = query(helper.getReadableDatabase());
    }

    //应用单例模式
    public static ContactLib get_lib(Context context) {
        if (holder == null) {
            holder = new ContactLib(context);
            return holder;
        } else {
            return holder;
        }
    }

    //增加一项
    public void addContact(Contact c) {
        //直接在数据库中增加一项

        ContentValues cv = getCV(c);
        SQLiteDatabase db = helper.getWritableDatabase();
        db.insert(ContactDBHelper.TABLE_NAME,null,cv);
        db.close();
        flush();
    }

    private ContentValues getCV(Contact c) {
        ContentValues cv = new ContentValues();
        cv.put("name",c.getName());
        cv.put("phone",c.getPhone());
        cv.put("address",c.getAddress());
        cv.put("photo", c.getPhoto());
        return cv;
    }

    //获取一项
    public Contact getContact(int i) {

        return collection.get(i);
    }

    //判空
    public boolean isEmpty() {
        return collection.isEmpty();
    }

    //删除一项
    public void delContact(Contact c) {

        SQLiteDatabase db = helper.getWritableDatabase();
        db.delete(ContactDBHelper.TABLE_NAME,"id=?",new String[]{String.valueOf(c.getId())});
        db.close();
        flush();
    }

    public int getIndex(Contact c) {
        return collection.indexOf(c);
    }

    //获取长度
    public int getSize() {
        return collection.size();
    }

    public void setContact(Contact c) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.update(ContactDBHelper.TABLE_NAME,getCV(c),"id=?",new String[]{String.valueOf(c.getId())});
        Log.d("=====sqlid===",c.toString());
        db.close();
        flush();

    }

    public List<Contact> searchContact(String name) {
        List<Contact> result = new ArrayList<>();
        for (int i = 0; i < collection.size(); i++) {
            if (collection.get(i).getName().indexOf(name) >= 0) {
                result.add(collection.get(i));
            }
        }
        return result;
    }

    private List<Contact> query(SQLiteDatabase db) {
        List<Contact> contacts = new ArrayList<>();;
        Cursor cursor = db.rawQuery("select * from " + ContactDBHelper.TABLE_NAME, null);
        if (cursor != null && cursor.getCount() > 0) {

            while (cursor.moveToNext()) {
                Contact contact = new Contact();
                contact.setId(cursor.getInt(cursor.getColumnIndex("id")));
                contact.setName(cursor.getString(cursor.getColumnIndex("name")));
                contact.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
                contact.setAddress(cursor.getString(cursor.getColumnIndex("address")));
                byte[] photo_bt = cursor.getBlob(cursor.getColumnIndex("photo"));
                contact.setPhoto(photo_bt);
                contacts.add(contact);
                Log.d("==contact=",contact.toString());
            }
            cursor.close();
        }
        db.close();
        return contacts;
    }


}
